$(function () {
    $("#hkjh_toolbar a[name='new']").click(function(){
        var gsId = $("#hkjh_toolbar [name='gsId']").val();
        var year =  $("#hkjh_toolbar [name='year']").val();
        var month = $("#hkjh_toolbar [name='month']").val();
        if(!gsId){
            $.messager.alert("警告","请先选择公司！","warning");
            return;
        }
        if(!year){
            $.messager.alert("警告","请选择年份！","warning");
            return;
        }
        if(!month){
            $.messager.alert("警告","请选择月份！","warning");
            return;
        }
        var win = $.window({
            title:'新建回款计划',
            width: 870,
            height: 470,
            cache: false,
            iconCls: 'icon-new',
            url:$.webPath() + '/jhgl/hkjh/hkjhmx_content?gsId=' + gsId +"&year="+year+"&month="+month,
            modal: true,
            buttons: [{text:'保存',handler:function(){
                win.mask();
                $("#hkjhmx table[name='datagrid']").edatagrid("saveRow");
                $("#hkjh_save").form("submit",{
                    onSubmit:function(param){
                        var rows = $("#hkjhmx table[name='datagrid']").datagrid("getRows");
                        if (!rows.length){
                            win.mask("hide");
                            $.messager.alert('警告','未添加任何明细','warning');
                            return false;
                        }
                        for(var i=0;i<rows.length;i++){
//                            alert($("#hkjhmx table[name='datagrid']").datagrid("validateRow",i));
                            if(!$("#hkjhmx table[name='datagrid']").datagrid("validateRow",i) || !rows[i].xh || !rows[i].cd || !rows[i].khid){
                                win.mask("hide");
                                $.messager.alert('警告','数据有误','warning');
                                return false;
                            }
                            param['hkjh.hkjhmxList['+i+'].khid'] = rows[i].khid;
                            param['hkjh.hkjhmxList['+i+'].xh'] = rows[i].xh;
                            param['hkjh.hkjhmxList['+i+'].cd'] = rows[i].cd;
                            param['hkjh.hkjhmxList['+i+'].bz'] = rows[i].bz;
                        }
                    },
                    success:function(data){
                        var obj = eval('('+data+')');
                        win.mask("hide");
                        if(obj.statusCode == 200){
                            $.messager.show({title:'提示',msg:'保存成功'});
                            //刷新
                            win.window("close");
                            $('#hkjh_grid').datagrid("reload");
                        } else {
                            $.messager.alert('提示',obj.message,'error');
                        }
                    }
                });
            }}]
        });
    });
    //======================
    //======设置查询条件========
    //======================
    //计划年份
    $("#hkjh_toolbar [name='year']").numberspinner({
        min: 2000,
        max: 2100,
        editable: true
    });
    //计划月份
    $("#hkjh_toolbar [name='month']").numberspinner({
        min: 1,
        max: 12,
        editable: true
    });

    //公司部门
//    $("#hkjh_toolbar [name='gsId']").gsjgtree(1);
    $("#hkjh_toolbar [name='gsId']").jgbmtree($("#hkjh_toolbar input[name='bmId']"),undefined,$("#hkjh_toolbar input[name='bmId']").val(),undefined);


    //搜索按钮
    $("#hkjh_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams: {
                year: $("#hkjh_toolbar [name='year']").val(),
                month: $("#hkjh_toolbar [name='month']").val(),
                gsId: $("#hkjh_toolbar [name='gsId']").val()
//                bmId: $("#hkjh_toolbar [name='bmId']").val()
            }});
    });

    $("#hkjh_toolbar a.new").click(function(){

    });

//======================
//========统计列表========
//======================
    var columns = [
        [
//            {field:'id',title:'单号',width:30,rowspan:2,align:'center'},
//            {field:'gsmc',title:'分公司',width:100,rowspan:2,align:'center'},
            {field:'khmc',title:'回款客户',width:100,rowspan:2,align:'center'},
            {title:'应收款(单位：元)',colspan:3},
            {title:'计划回款(单位：万元)',colspan:3},
            {title:'销售回款(单位：万元)',colspan:3},
            {field:'dxl',title:'兑现率',width:50,rowspan:2,align:'center'},
            {field:'zdrq',title:'制单日期',width:50,rowspan:2,align:'center'},
            {field:'xm',title:'制单人姓名',width:50,rowspan:2,align:'center'}
        ],
        [
            {field: 'wq', title: '应收款上期', width: 60,align:'right'},
            {field: 'bq', title: '应收款本期', width: 60,align:'right'},
            {field: 'yskxj', title: '应收款小计', width: 60,align:'right'},
            {field: 'jhxh', title: '计划回款现汇', width: 60,align:'right'},
            {field: 'jhcd', title: '计划回款承兑', width: 60,align:'right'},
            {field: 'jhxj', title: '计划回款小计', width: 60,align:'right'},
            {field: 'sjxh', title: '销售回款现汇', width: 60,align:'right'},
            {field: 'sjcd', title: '销售回款承兑', width: 60,align:'right'},
            {field: 'sjxj', title: '销售回款小计', width: 60,align:'right'}
        ]
    ];

    var grid = $('#hkjh_grid').datagrid({
        url: $.webPath() + '/jhgl/hkjh/hkjhReport',
        toolbar: "#hkjh_toolbar",
        rownumbers: true,
        singleSelect: true,
        fit: true,
        fitColumns: true,
        pageNumber: 1,
        showFooter: true,
        queryParams: {
            year: $("#hkjh_toolbar [name='year']").val(),
            month: $("#hkjh_toolbar [name='month']").val()
        },
        columns: columns,
        view:groupview,
        groupField:'gsid',
        groupFormatter:function(value,rows){
            return '计划单号:'+'<a style="color:red;cursor:pointer;">'+rows[0].jhdh+ '</a>' + ',&nbsp;&nbsp;分公司:'+rows[0].gsmc+ ' - ' + rows.length + ' 条记录';
        },
        onClickRow: function (rowIndex, rowData) {
        }
    });
//===========================================
//==============此页面用到的函数===================
//===========================================


});
